package com.chixing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chixing.commons.ServerResponse;
import com.chixing.mapper.CompanyDetailMapper;
import com.chixing.mapper.JobDetailMapper;
import com.chixing.pojo.CompanyDetail;
import com.chixing.pojo.CompanyManage;
import com.chixing.mapper.CompanyManageMapper;
import com.chixing.pojo.JobDetail;
import com.chixing.service.ICompanyManageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 高管信息表 服务实现类
 * </p>
 *
 * @author Jason Marslyon
 * @since 2022-10-04
 */
@Service
public class CompanyManageServiceImpl  implements ICompanyManageService {

    @Autowired
    private CompanyManageMapper companyManageMapper;
    @Autowired
    private JobDetailMapper jobDetailMapper;

//jobdetail页面需要高管的    姓名  职位   头像
    @Override
    public ServerResponse getManagerMsg(int jobId) {
        JobDetail jobDetail = jobDetailMapper.selectById(jobId);
        int manageId=jobDetail.getManageId();
        QueryWrapper<CompanyManage> wrapper=new QueryWrapper<>();
        wrapper.select("manage_name","manage_position","manage_img");
        wrapper.eq("manage_id",manageId);
        CompanyManage companyManage = companyManageMapper.selectOne(wrapper);
        if (companyManage!=null)
            return ServerResponse.success("查询高管姓名，职位，头像成功",companyManage);
        return ServerResponse.fail("查询高管姓名，职位，头像成功",null);
    }

    @Override
    public CompanyManage checkLogin(String username, String password) {
        QueryWrapper<CompanyManage> wrapper = new QueryWrapper<>();
        wrapper.eq("manage_username",username);
        wrapper.eq("manage_password",password);
        List<CompanyManage> companyManages = companyManageMapper.selectList(wrapper);//查询多个
        if(companyManages.size()==1){
            return companyManages.get(0);
        }
        return null;
    }

    @Override
    public boolean insertManage(CompanyManage companyManage) {
        int insert = companyManageMapper.insert(companyManage);
        return insert>0;
    }

    @Override
    public boolean updateManage(CompanyManage companyManage) {
        int i = companyManageMapper.updateById(companyManage);
        return i>0;
    }

    @Override
    public CompanyManage selectOrderManagePass(String oldPassword, HttpSession session) {
        QueryWrapper<CompanyManage> wrapper = new QueryWrapper<>();
        wrapper.eq("manage_password",oldPassword);
        List<CompanyManage> companyManages = companyManageMapper.selectList(wrapper);
        CompanyManage loginuser = (CompanyManage) session.getAttribute("loginuser");
        System.out.println(session);
        System.out.println("aa"+loginuser);
        for (CompanyManage companyManage : companyManages) {
            if(companyManage.getManageId().equals(loginuser.getManageId())) {
                return companyManage;
            }
        }
        return null;
    }

    @Override
    public boolean updatePass(String newPassword, HttpSession session) {
        CompanyManage loginUser = (CompanyManage) session.getAttribute("loginuser");
        loginUser.setManagePassword(newPassword);
        session.setAttribute("loginuser", loginUser);
        int i = companyManageMapper.updateById(loginUser);
        return i>0;
    }
}
